package swingUI;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.swing.JOptionPane;

import com.trsjh.db.controller.Home;
import com.trsjh.db.domains.CustomerAccount;
import com.trsjh.db.domains.CustomerRecord;

import mySwingToolkit.controller.TableController;
import mySwingToolkit.model.ClassModel;
import mySwingToolkit.model.SwitchedTableModel;
import mySwingToolkit.ui.TableView;


@SuppressWarnings("serial")
public class CustomerRecordViewUI extends TableView<CustomerRecord> {
	CustomerAccount ca;
	// SwitchedTableModel<CustomerRecord> sm=;
	@SuppressWarnings("unchecked")
	public CustomerRecordViewUI(CustomerAccount ca1) {
		super(new SwitchedTableModel<CustomerRecord>(
				new Object[][] {
				},
				new String[] {
						"时间", "左眼",  "左散光度数", "左轴向","右眼","右散光度数","右轴向", "瞳距","获得积分","备注"
				},
				new Class[] {
						Date.class, Float.class, Float.class, Float.class, Float.class, Float.class,Float.class, Float.class,Float.class,String.class
				},
				new boolean[] {
						false,true,true,true,true,true,true,true,true,true
				},false,false,true,20
				){
			@Override
			public Object[] translate(ClassModel<CustomerRecord> customerRecord) {
				CustomerRecord c=customerRecord.getObject();		

				return new Object[]{customerRecord,getRowCount()+1,c.getTime(),c.getLeftSight(),c.getLeft2(),c.getLeft21(),c.getRightSight(),c.getRight2(),c.getRight21(),c.getDistance(),c.getScore(),c.getNote()};
			}

			@Override
			public CustomerRecord constructObject(int row, int col) {
				// TODO Auto-generated method stub
				ClassModel<CustomerRecord> cm=(ClassModel<CustomerRecord>) this.getValueAt(row, 0);
				CustomerRecord cr=null;
				if(cm!=null){
					cr=cm.getObject();
				}else{
					cr=new CustomerRecord();
					cr.setTime(new Date());
				}
				switch(col){
				case 2:
					break;
				case 3:
					cr.setLeftSight((Float)this.getValueAt(row, col));
					break;
				case 4:
					cr.setLeft2((Float)this.getValueAt(row, col));
					break;
				case 5:
					cr.setLeft21((Float)this.getValueAt(row, col));
					break;
				case 6:
					cr.setRightSight((Float)this.getValueAt(row, col));
					break;
				case 7:
					cr.setRight2((Float)this.getValueAt(row, col));
					break;
				case 8:
					cr.setRight21((Float)this.getValueAt(row, col));
					break;
				case 9:
					cr.setDistance((Float)this.getValueAt(row, col));
					break;
				case 10:
					cr.setScore((Float)this.getValueAt(row, col));
					break;
				case 11:
					cr.setNote((String)this.getValueAt(row, col));
					break;
				}
				return cr;
			}
			@Override
			public void initNullRow(int row) {
				// TODO Auto-generated method stub
				
			}
		});
		this.ca=ca1;
		Home home=new Home();
		Map<Integer,Object> params=new HashMap<Integer,Object>();
		params.put(1, ca);
		this.initDatas(home.list(Home.LIST_CUSTOMER_RECORDS, params));
		home.closeEntityManager();
		this.setTableController(new TableController<CustomerRecord>(){
			@Override
			public List<CustomerRecord> getBackupDatas(
					SwitchedTableModel<CustomerRecord> tm) {
				JOptionPane.showMessageDialog(null, "暂不支持");
				return null;
			}
			@Override
			public boolean saveChanges(SwitchedTableModel<CustomerRecord> tm) {
				System.out.println(tm.getInsertDatas().size());
				Home home=new Home();
				try{
					home.beginTransaction();
					for(ClassModel<CustomerRecord> cm:tm.getInsertDatas()){
						CustomerRecord cr=cm.getObject();
						cr.setCustomerAccount(ca);
						cr.setTime(new Date());
						home.persist(cr);
						ca.setTotalScore(ca.getTotalScore()+cr.getScore());
						ca.setLeftScore(ca.getLeftScore()+cr.getScore());
						home.merge(ca);
					}
					home.commit();
					return true;
				}catch(Exception e){
					home.rollBack();
					return false;
				}finally{
					home.closeEntityManager();
				}
			}
		});
	}
}
